/**
 * 
 */
package org.json.simple;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

/**
 * A JSON array. JSONArray supports java.util.List interface.
 * 
 * @author Roger Xu<roger.xu.2007@gmail.com>
 */
public class JSONArray extends ArrayList<Object> implements JSONAware {

    private static final long serialVersionUID = -3272086449502602059L;

    public JSONArray() {
        super();
    }

    /**
     * Allows creation of a JSONArray from a List.
     * 
     * @param list
     */
    public JSONArray(final List<Object> list) {
        super(list);
    }

    /**
     * Encode a list into JSON text and write it to out. If this list is also a JSONStreamAware or a JSONAware,
     * JSONStreamAware and JSONAware specific behaviors will be ignored at this top level.
     * 
     * @see org.json.simple.JSONValue#writeJSONString(Object, Writer)
     * 
     * @param list
     *            The list object.
     * @param out
     *            The Writer object.
     * @param isPretty
     *            true if output in a pretty style format
     * @throws IOException
     */
    public static void writeJSONString(final List<Object> list, final Writer out, final boolean isPretty)
            throws IOException {
        if (list == null) {
            out.append("null");
            return;
        }

        boolean first = true;

        out.append('[');
        for (final Object value : list) {
            if (first) {
                first = false;
            } else {
                out.append(',');
                if (isPretty) {
                    out.append(' ');
                }
            }
            JSONValue.writeJSONString(value, out, isPretty);
        }
        out.append(']');
    }

    @Override
    public void writeJSONString(final Writer out, final boolean isPretty) throws IOException {
        writeJSONString(this, out, isPretty);
    }

    /**
     * Convert a list to JSON text. The result is a JSON object. If this map is also a JSONAware, JSONAware specific
     * behaviors will be omitted at this top level.
     * 
     * @see org.json.simple.JSONValue#toJSONString(Object)
     * 
     * @param list
     *            The list object.
     * @param isPretty
     *            true if output in a pretty style format
     * @return JSON text, or "null" if list is null.
     */
    public static String toJSONString(final List<Object> list, final boolean isPretty) {
        final StringBuilder sb = new StringBuilder();
        try {
            final JSONWriter<StringBuilder> out = new JSONWriter<StringBuilder>(sb);
            writeJSONString(list, out, isPretty);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return sb.toString();
    }

    @Override
    public String toJSONString() {
        return toJSONString(this, false);
    }

    @Override
    public String toJSONString(final boolean isPretty) {
        return toJSONString(this, isPretty);
    }

    @Override
    public String toString(){
        return toJSONString(false);
    }
}
